<?php
/**
 * common model file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/08/20
 * Time: 06:01
 */

namespace HiCommon\Model;

class Activity extends Base
{
    
    protected $table = 'activity';
    
    protected $guarded = [];
    
    //public $timestamps = false;
    
    /**
     * 应该被调整为日期的属性
     *
     * @var array
     */
    protected $dates = [
    ];
    
  /**
     * 获取状态对应的名称
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getStateAttribute($value)
    {
        switch ($value)
        {
            case 0:
                $res = ['id'=>0,'name'=>'草稿'];
                break;
            case 1:
                $res = ['id'=>1,'name'=>'待审核'];
                break;
            case 2:
                $res = ['id'=>2,'name'=>'通过'];
                break;
            case 3:
                $res = ['id'=>3,'name'=>'拒绝'];
                break;
            default:
                $res = ['id'=>-1,'name'=>'未知'];
        }
        return $res;
    }
        


        /**
         * 获取图片可用链接
         *
         * @return array
         * @author wumengmeng <wu_mengmeng@foxmail.com>
         */
        public function getImgAttribute($value)
        {   
            $arr_data = [
              'src'=>$value,
              'full_src'=>oss_full_url($value),
            ];
            return $arr_data;
        }


        /**
         * 获取活动类型
         *
         * @return array
         * @author wumengmeng <wu_mengmeng@foxmail.com>
         */
        public function getTypeAttribute($value)
        {   
            switch ($value)
            {
                case 1:
                    $res = ['type'=>$value,'type_msg'=>'满减'];
                    break;
                case 2:
                    $res = ['type'=>$value,'type_msg'=>'促销'];
                    break;
                default:
                    $res = ['type'=>-1,'type_msg'=>'未知'];
            }
            return $res;
        }
        

        
        /**
         * 获取活动状态
         *
         * @return array
         * @author wumengmeng <wu_mengmeng@foxmail.com>
         */
        public function getStatusAttribute($value)
        {   
            switch ($value)
            {
                case 1:
                    $res = ['status'=>$value,'status_msg'=>'正常'];
                    break;
                case 2:
                    $res = ['status'=>$value,'status_msg'=>'禁用'];
                    break;
                case 3:
                    $res = ['status'=>$value,'status_msg'=>'平台禁用'];
                    break;
                case 4:
                    $res = ['status'=>$value,'status_msg'=>'活动过期'];
                    break;
                default:
                    $res = ['status'=>-1,'status_msg'=>'未知'];
            }
            return $res;
        }


        /**
         * 扩展信息
         *
         * @return array
         * @author wumengmeng <wu_mengmeng@foxmail.com>
         */
        public function getExtendAttribute(){
            $s_created_at = $this->created_at;
            $arr_data = [
              'created_at'=>ymd_friendly_date($s_created_at),
            ];
            return $arr_data;
        }

        

        /**
         *  关联信息
         *  hinq 2019 09 05
         *
         **/
        public function activity_extend(){
            return $this->hasMany(ActivityExtend::class,'activity_id')->orderBy('price_all','asc');
        }



        /**
         *  关联信息
         *  hinq 2019 09 05
         *
         **/
        public function activity_goods_extend(){
            return $this->hasMany(ActivityGoodsExtend::class,'activity_id')->select('id','activity_id','name','goods');
        }

        /**
         *  关联信息
         *  hinq 2019 09 05
         *
         **/
        public function shop(){
            return $this->belongsTo(Shop::class,'repairstore_id')->select('id','name');
        }

        /**
         *  关联信息
         *  hinq 2020 04 12
         *
         **/
        public function push(){
            return $this->hasMany(ActivityPush::class,'activity_id')->where('type',1)->select('id','activity_id','push_time');
        }

}